package com.simen.hellobytes;

import com.simen.hellobytes.http.HttpHeaderUtil;

import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;

/**
 * @author Created by Simen.
 * @date 创建日期 2018/11/11 19:35
 * @modify 修改者 Simen
 */
public class URLUtil {

    /**
     * @return {@code true} if the url is an http: url.
     */
    public static boolean isHttpUrl(String url) {
        return (null != url) &&
                (url.length() > 6) &&
                url.substring(0, 7).equalsIgnoreCase("http://");
    }

    /**
     * @return {@code true} if the url is an https: url.
     */
    public static boolean isHttpsUrl(String url) {
        return (null != url) &&
                (url.length() > 7) &&
                url.substring(0, 8).equalsIgnoreCase("https://");
    }

    /**
     * @return {@code true} if the url is a network url.
     */
    public static boolean isNetworkUrl(String url) {
        if (url == null || url.length() == 0) {
            return false;
        }
        return isHttpUrl(url) || isHttpsUrl(url);
    }

    public static String guessFileName(String url, String contentDisposition) {
        return guessFileName(url,contentDisposition,null);
    }

    public static String guessFileName(String url, String contentDisposition,String defaultName) {
        String filename = null;

        // If we couldn't do anything with the hint, move toward the content disposition
        if (contentDisposition != null) {
            filename = HttpHeaderUtil.parseFileNameFromContentDisposition(contentDisposition);
            if (filename != null) {
                int index = filename.lastIndexOf('/') + 1;
                if (index > 0) {
                    filename = filename.substring(index);
                }
            }
        }

        // If all the other http-related approaches failed, use the plain uri
        if (filename == null) {
            String decodedUrl = null;
            try {
                decodedUrl = URLDecoder.decode(url,"UTF-8");
            } catch (UnsupportedEncodingException e) {
                try {
                    decodedUrl = URLDecoder.decode(url,"GBK");
                } catch (UnsupportedEncodingException e1) {
                    decodedUrl = URLDecoder.decode(url);
                }
            }
            if (decodedUrl != null) {
                int queryIndex = decodedUrl.indexOf('?');
                // If there is a query string strip it, same as desktop browsers
                if (queryIndex > 0) {
                    decodedUrl = decodedUrl.substring(0, queryIndex);
                }
                if (!decodedUrl.endsWith("/")) {
                    int index = decodedUrl.lastIndexOf('/') + 1;
                    if (index > 0) {
                        filename = decodedUrl.substring(index);
                    }
                }
            }
        }

        // Finally, if couldn't get filename from URI, get a generic filename
        if (filename == null) {
            filename = defaultName;
        }

        return filename;
    }


}
